package com.tkjt.pms.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class EsDateUtils {

	public static final String S_FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final SimpleDateFormat fullFormat = new SimpleDateFormat(S_FULL_FORMAT);

	public static String format(Date date) {
		if (date != null) {
			String dateString = fullFormat.format(date);
			return dateString;
		} else {
			return "";
		}
	}

	public static String formatPattern(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat fullFormat = new SimpleDateFormat(pattern);
			String dateString = fullFormat.format(date);
			return dateString;
		} else {
			return "";
		}
	}

	public static Date parse(String date) {
		return parsePattern(date, S_FULL_FORMAT);
	}

	public static Date parsePattern(String dateStr, String pattern) {
		if (pattern == null || "".equals(pattern)) {
			pattern = S_FULL_FORMAT;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/*
	 * 增加年数
	 */
	public static Date addYears(Date date, int years) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, years);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加月数
	 */
	public static Date addMonths(Date date, int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, months);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加天数
	 */
	public static Date addDays(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, days);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加分钟数
	 */
	public static Date addMinutes(Date date, int minute) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minute);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加毫秒数
	 */
	public static Date addMilliSeconds(Date date, int milliSeconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, milliSeconds);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加秒数
	 */
	public static Date addSeconds(Date date, int seconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, seconds);
		date = cal.getTime();
		return date;
	}

	/*
	 * 增加小时数
	 */
	public static Date addHours(Date date, int hours) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hours);
		date = cal.getTime();
		return date;
	}

	/**
	 * 将年月日的日期 转化成 20180203 格式的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getStringFromDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH));
		if ("0".equals(month)) {
			month = "12";
		} else if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));

		return year + month + day;
	}

	/**
	 * 获取指定时间的秒数 例如 2018-10-26 11:05:04.336 返回336
	 * 
	 * @return
	 */
	public static int getMilliSeconds(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.MILLISECOND);
	}

	/**
	 * 获取指定时间的秒数 例如 2018-10-26 11:05:04 返回4
	 * 
	 * @return
	 */
	public static int getSeconds(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.SECOND);
	}

	/**
	 * 获取指定时间的秒数 例如 2018-10-26 11:05:04 返回5
	 * 
	 * @return
	 */
	public static int getMinutes(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.MINUTE);
	}

	/**
	 * 获取指定时间的月份 例如 2018-10-26 11:05:04 返回10
	 * 
	 * @return
	 */
	public static int getMonths(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.MONTH) + 1;
	}

	public static int getYears(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取指定时间的月份(两位字符，小于10数据，第一位使用0填充) 例如 2018-01-26 11:05:04 返回10
	 * 
	 * @return
	 */
	public static String getLastMonthStr(Date date) {
		StringBuilder sbuf = new StringBuilder();
		int month = getMonths(date);
		month -= 1;
		if (month == 0) {
			month = 12;
		}
		if (month < 10) {
			sbuf.append("0");
		}
		sbuf.append(month);
		return sbuf.toString();
	}

	public static String getMonthStr(Date date) {
		StringBuilder sbuf = new StringBuilder();
		int month = getMonths(date);
		//month -= 1;
		if (month == 0) {
			month = 12;
		}
		if (month < 10) {
			sbuf.append("0");
		}
		sbuf.append(month);
		return sbuf.toString();
	}
	
	public static String getIndexYearMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH );
		String monStr = String.valueOf(month);
		if("0".equals(monStr)){
			monStr = "12";
			year = year -1;
		}else if(monStr.length() == 1) {
			monStr = "0"+monStr;
		}
		return "" + year + "" + monStr;
	}
	
	public static String getIndexYear(String indexYearMonth) {
		return indexYearMonth.substring(0, 4);
	}
	
	public static String getIndexMonth(String indexYearMonth) {
		return indexYearMonth.substring(4, 6);
	}
	
	public static String getIndexYearMonth(String year, String month) {
		return year + "" + month;
	}



    public final static char[] upper = "零一二三四五六七八九十".toCharArray();
	public static String getMonthToUp(int month){
        if(month <= 10) {
            return upper[month]+"月";
        } else {
            return "十"+upper[month%10]+"月";
        }
    }

    public static void main(String[] args) {
        System.out.println(getLastMonthStr(new Date()));
    }
}
